<?php
/**
 * Created by PhpStorm.
 * User: Sky
 * Date: 2017/8/7
 * Time: 下午5:12
 */

namespace app\controllers;


use app\librarys\BaseController;
use app\models\File;
use app\models\Group;
use app\models\MerchantGroup;
use yii\data\Pagination;

class GroupController extends BaseController
{
    // region 群组
    public function actionGroupList()
    {
        $count = Group::getCount();
        $pagination = new Pagination(['totalCount' => $count]);
        $items = Group::getList($pagination->page, $pagination->pageSize);
        return $this->render('group-list',
            [
                'items' => $items,
                'pagination' => $pagination
            ]
        );
    }

    public function actionGroupDetailPartial()
    {
        $id = \Yii::$app->request->post('id');
        $item = null;
        if ($id != '0') {
            $item = Group::getById($id);
            if (isset($item['image_id'])) {
                $item['show_image'] = File::getImageById($item['image_id']);
            }
        }
        return $this->renderPartial('group-detail-partial',
            ['id' => $id, 'item' => $item]);
    }

    public function actionGroupSaveHandler()
    {

        $id = \Yii::$app->request->post('group_id');
        $name = \Yii::$app->request->post('group_name');
        $remark = \Yii::$app->request->post('group_remark');

        $show_image_id = \Yii::$app->request->post('show_image_id');
        if (!empty($_FILES['uploadImg']['name'])) {
            if ($_FILES['uploadImg']['error'] == 0) {
                $imgCord = json_decode(\Yii::$app->request->post('imgCord'));
                if ($imgCord) {
                    $show_image_id = File::CutUploadImage($_FILES['uploadImg'], $imgCord->x, $imgCord->y, $imgCord->w, $imgCord->h, $imgCord->size_w, $imgCord->size_h);
                }
                if (!$show_image_id) {
                    return $this->renderJson('', 1, '图片上传错误');
                }
            }
        }

        Group::save($id, $name, $show_image_id, $remark);

        return $this->renderJson();
    }
    // endregion

    // region 群组成员
    public function actionGroupMemberList()
    {
        $groupId = \Yii::$app->request->get('group-id');
        $count = Group::getGroupMemberCountByGroupId($groupId);
        $pagination = new Pagination(['totalCount' => $count]);
        $items = Group::getGroupMemberList($pagination->page, $pagination->pageSize, $groupId);

        $groupItems = Group::getList(0, 1000);
        return $this->render('group-member-list',
            [
                'items' => $items,
                'group_items' => $groupItems,
                'pagination' => $pagination
            ]
        );
    }
    // endregion

    //region 商户平台
    public function actionMerchantGroupList()
    {
        $count = MerchantGroup::getCount();
        $pagination = new Pagination(['totalCount' => $count]);
        $items = MerchantGroup::getList($pagination->page, $pagination->pageSize);
        return $this->render('merchant-group-list',
            [
                'items' => $items,
                'pagination' => $pagination
            ]
        );
    }

    public function actionMerchantGroupDetailPartial()
    {
        $id = \Yii::$app->request->post('id');
        $item = null;
        if ($id != '0') {
            $item = MerchantGroup::getById($id);
        }
        return $this->renderPartial('merchant-group-detail-partial',
            ['id' => $id, 'item' => $item]);
    }

    public function actionMerchantGroupSaveHandler()
    {
        $id = \Yii::$app->request->post('id');
        $code = \Yii::$app->request->post('code');
        $name = \Yii::$app->request->post('name');

        MerchantGroup::save($id, $code, $name);

        return $this->renderJson();
    }

    public function actionMerchantGroupMerchantList()
    {
        $merchantGroupId = \Yii::$app->request->get('merchant-group-id');
        $count = MerchantGroup::getMerchantCountByGroupId($merchantGroupId);
        $pagination = new Pagination(['totalCount' => $count]);
        $items = MerchantGroup::getMerchantList($pagination->page, $pagination->pageSize, $merchantGroupId);

        $groupItems = MerchantGroup::getList(0, 100);
        return $this->render('merchant-group-merchant-list',
            [
                'items' => $items,
                'group_items' => $groupItems,
                'pagination' => $pagination
            ]
        );
    }
    // endregion
}